package cc.zy.base.hr.controller;

import cc.zy.base.common.annotation.ControllerEndpoint;
import cc.zy.base.common.controller.BaseController;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.exception.FebsException;
import cc.zy.base.common.utils.DateUtil;
import cc.zy.base.common.utils.FebsUtil;
import cc.zy.base.hr.entity.Lecturer;
import cc.zy.base.hr.entity.OutsideTeacher;
import cc.zy.base.hr.service.IInsideTeacherService;
import cc.zy.base.hr.service.IOutsideTeacherService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @author 郝秀峰
 * @time 2022/03/08 19:27:30
 */
@Slf4j
@Validated
@Controller("hrView_outsideTeacher")
@RequiredArgsConstructor
public class OutsideTeacherController extends BaseController {

    private static final Integer MAXID = 63000;
    private static final Integer MINID = 60000;


    @Resource
    private final IOutsideTeacherService outsideTeacherService;

    private final IInsideTeacherService insideTeacherService;

    @GetMapping("outsideTeacher/list")
    @ResponseBody
    public FebsResponse outsideTeacherList(QueryRequest request, OutsideTeacher outsideTeacher) {

        log.debug("前台传来的对象是：" + outsideTeacher);

        Map<String, Object> dataTable = getDataTable(outsideTeacherService.findOutsideTeachers(request, outsideTeacher));

        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping("outsideTeacher/delete/{outsideTeacherIds}")
    @ControllerEndpoint(operation = "删除外部讲师信息", exceptionMessage = "删除外部讲师信息异常")
    @ResponseBody
    public FebsResponse deleteOutsideTeachers(@NotBlank(message = "{required}") @PathVariable String outsideTeacherIds) {

        String[] ids = outsideTeacherIds.split(StringPool.COMMA);

        outsideTeacherService.deleteOutsideTeachers(ids);

        return new FebsResponse().success();
    }

    @PostMapping("outsideTeacher/update")
    @ControllerEndpoint(operation = "修改外部讲师信息", exceptionMessage = "修改外部讲师信息异常")
    @ResponseBody
    public FebsResponse updateOutsideTeacher(@Valid OutsideTeacher outsideTeacher, String bankPic) {

        log.debug("outsideTeacher",outsideTeacher,bankPic);

        if (outsideTeacher.getId() == null) {
            throw new FebsException("用户ID为空");
        }

        outsideTeacher.setLastModifyDate(new Date());
        outsideTeacher.setBankPic(bankPic);
        outsideTeacher.setLastModifyUserId(FebsUtil.getCurrentUser().getUserId());
        outsideTeacherService.updateOutsideTeacher(outsideTeacher);

        return new FebsResponse().success();
    }


    @GetMapping("outsideTeacher/excel")
    @ControllerEndpoint(exceptionMessage = "导出Excel异常")
    public void export(HttpServletResponse response, OutsideTeacher outsideTeacher) {

        log.debug(String.format("response = %s", response));
        log.debug(String.format("outsideTeacher = %s", outsideTeacher));

        List<OutsideTeacher> outsideTeachers = outsideTeacherService.findOutsideTeacherListNotPage(outsideTeacher);

        ExcelKit.$Export(OutsideTeacher.class, response).downXlsx(outsideTeachers, false);
    }

    /**
     * 方法描述：查找外部讲师表
     *
     * @param outsideTeacherId 外部讲师id
     * @return cc.zy.base.common.entity.FebsResponse
     * @author houweikang
     * @date 2021/2/3
     */
    @GetMapping("outsideTeacher/getById/{outsideTeacherId}")
    @ResponseBody
    public FebsResponse getById(@PathVariable Integer outsideTeacherId) {

        log.debug(String.format("outsideTeacherId = %s", outsideTeacherId));

        OutsideTeacher outsideTeacher = outsideTeacherService.getById(outsideTeacherId);

        return new FebsResponse().success().data(outsideTeacher);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "outsideTeacher/detail/{id}")
    public String outsideTeacherDetail(@PathVariable Integer id, Model model) {

        resolveoutsideTeacherModel(id, model, true);

        return FebsUtil.hrView("outsideTeacher/outsideTeacherDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "outsideTeacher/update/{id}")
    public String outsideTeacherUpdate(@PathVariable Integer id, Model model) {

        resolveoutsideTeacherModel(id, model, false);

        return FebsUtil.hrView("outsideTeacher/outsideTeacherUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "outsideTeacher/add")
    public String outsideTeacherAdd() {
        return FebsUtil.hrView("outsideTeacher/outsideTeacherAdd");
    }

    // 增加
    @PostMapping("outsideTeacher/add")
    @ResponseBody
    @ControllerEndpoint(operation = "新增外部讲师信息\", exceptionMessage = \"新增外部讲师信息")
    public FebsResponse addOutsideTeacher(@Valid OutsideTeacher outsideTeacher,String bankPic) {

        log.debug("outsideTeacher", outsideTeacher,bankPic);
        // 不能为非中文，不能为空
        String regex = "^[\u4e00-\u9fa5]+$";
        Boolean b = FebsUtil.match(regex,outsideTeacher.getRealName());
        // 判断传入的负责人是否包含非中文
        if (!b){
            throw new FebsException("姓名包含非中文！");
        }

        outsideTeacher.setCreateUserId(FebsUtil.getCurrentUser().getUserId());
        outsideTeacher.setLastModifyUserId(FebsUtil.getCurrentUser().getUserId());
        outsideTeacher.setStop(1);
        outsideTeacher.setBankPic(bankPic);
        outsideTeacherService.createOutsideTeacher(outsideTeacher);

        return new FebsResponse().success();
    }

    // 判断重复
    @GetMapping("outsideTeacher/isRepetitive")
    @ResponseBody
    public Boolean isRepetitive(String realName,String phone) {

       return outsideTeacherService.isRepetitive(realName,phone);
    }

    private void resolveoutsideTeacherModel(Integer id, Model model, Boolean transform) {

        OutsideTeacher outsideTeacher = outsideTeacherService.getById(id);

        if (outsideTeacher.getCreateDate() != null) {
            model.addAttribute("createDateTime", DateUtil.getDateFormat(outsideTeacher.getCreateDate(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
        if (outsideTeacher.getLastModifyDate() != null) {
            model.addAttribute("updateDateTime", DateUtil.getDateFormat(outsideTeacher.getLastModifyDate(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
        System.out.println(outsideTeacher.getBankPic());
        model.addAttribute("outsideTeacher", outsideTeacher);
    }

    // 修改
    @GetMapping("outsideTeacher/updateStatus/{outsideTeacherIds}")
    @ControllerEndpoint(operation = "修改外部讲师状态", exceptionMessage = "修改外部讲师状态异常")
    @ResponseBody
    public FebsResponse updateOutsideTeacherStatus(@NotBlank(message = "{required}") @PathVariable String outsideTeacherIds) {
        if (outsideTeacherIds == null){
            return new FebsResponse().fail().message("id不能为null");
        }

        Integer outsideTeacherId = Integer.valueOf(outsideTeacherIds);
        outsideTeacherService.updateOutsideTeacherStatus(outsideTeacherId);
        return new FebsResponse().success();
    }

    /**
     * @Author: PW
     * @Description:
     * @DateTime: 2022/3/7 16:24
     * @Params: [request, staffLabel]
     * @Return cc.zy.base.common.entity.FebsResponse
     */
    @GetMapping("outsideTeacher/selectList")
    @ResponseBody
    public FebsResponse OutsideTeacherlList(QueryRequest request, Lecturer lecturer) {
        log.debug("前台传来的对象是：" + lecturer);
        Map<String, Object> dataTable;
        if ("0".equals(lecturer.getLecturerType())) {
            dataTable = getDataTable(outsideTeacherService.listOutsideTeacher(request, lecturer));
        }else if ("1".equals(lecturer.getLecturerType())){
            dataTable = getDataTable(insideTeacherService.listInsideTeacherDetailPage(request, lecturer));
        }else {
            dataTable = getDataTable(outsideTeacherService.listOutsideTeacher(request, lecturer));
        }
        return new FebsResponse().success().data(dataTable);
    }
}